package com.doubledr.light_lock;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.doubledr.light_lock.utils.ShareRefHelper;

public class SettingPwdActivity extends AppCompatActivity {

    private TextView mOldPwdInput;
    private TextView mNewPwdInput;
    private TextView mTxtOldPwdLab;
    private Button   mBtnSave;
    private ImageButton mBtnBack;
    boolean isSettingAlready;
    private String  mID;
    private String mType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_pwd);
        Intent intent=getIntent();
        mType = intent.getStringExtra("type");
        init();
        bindEvent();
    }
    private void init(){
        mOldPwdInput = findViewById(R.id.edtOldPwd);
        mNewPwdInput = findViewById(R.id.edtNewPwd);
        mBtnSave = findViewById(R.id.btnSavePwd);
        mBtnBack = findViewById(R.id.btnBack2);
        mTxtOldPwdLab = findViewById(R.id.textOldPwdView);
        if(mType.contentEquals("FILE")){
            mID = ShareRefHelper.getFileID();
        }
        else{
            mID = ShareRefHelper.getFolderID();
        }
        String oldPwd = ShareRefHelper.getValue(mID);
        if(!oldPwd.contentEquals("")){
            isSettingAlready = true;
        }
        else{
            isSettingAlready = false;
        }
        //第一次设置
        if(!isSettingAlready){
            mOldPwdInput.setVisibility(View.INVISIBLE);
            mTxtOldPwdLab.setVisibility(View.INVISIBLE);
        }
    }
    private void bindEvent(){
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldpwd = mOldPwdInput.getText().toString();
                String newpwd = mNewPwdInput.getText().toString();
                if(newpwd.trim().toString().equals("")){
                    Toast toast=Toast.makeText(SettingPwdActivity.this,R.string.pwd_cannot_null,Toast.LENGTH_SHORT    );
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return;
                }
                if(isSettingAlready){
                    String pwdSaved = ShareRefHelper.getValue(mID);
                    if(!oldpwd.contentEquals(pwdSaved)){
                        Toast toast=Toast.makeText(SettingPwdActivity.this, R.string.oldpwd_incorect,Toast.LENGTH_SHORT    );
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                        return;
                    }
                }
                ShareRefHelper.saveValue(mID,newpwd);
                Toast toast=Toast.makeText(SettingPwdActivity.this, R.string.setting_pwd_success,Toast.LENGTH_SHORT    );
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingPwdActivity.this.finish();
            }
        });
    }
}
